package com.um.jdy.platform.shop.controller;

import com.um.jdy.common.shop.service.ShopOrderService;
import com.um.jdy.platform.shop.entity.dto.DeliveryDTO;
import com.um.jdy.platform.shop.entity.dto.ShopOrderExportDTO;
import com.um.jdy.platform.shop.entity.dto.ShopOrderPageDTO;
import com.um.jdy.platform.shop.entity.vo.ShopOrderDetailVO;
import com.um.jdy.platform.shop.entity.vo.ShopOrderItemVO;
import com.um.jdy.platform.shop.manager.ShopOrderManager;
import com.um.jdy.common.shop.entity.po.Order;
import com.um.jdy.common.shop.entity.dto.OrderDTO;
import com.um.jdy.common.shop.entity.vo.OrderVO;
import com.um.springboot.starter.annotation.ApiPermission;
import com.um.springboot.starter.annotation.TokenRequired;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.controller.BasePlusController;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.params.ExportParams;
import com.um.springboot.starter.orm.params.ListDTO;
import com.um.springboot.starter.orm.params.PageDTO;
import com.um.springboot.starter.orm.params.PageParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 控制器：订单表
 */
@RestController
@RequestMapping("/api/v1/cms/shop/order")
@Api(tags = "GoodsOrderController", description = "订单表")
@ApiPermission("shop_order")
@TokenRequired
public class ShopOrderController extends BasePlusController<ShopOrderManager, ShopOrderService, Order, OrderDTO, OrderVO, PageDTO, ListDTO> {

    @ApiPermission("page")
    @ApiOperation("分页查询订单")
    @PostMapping("get_page")
    public Response<Page<ShopOrderItemVO>> getPage(@RequestBody ShopOrderPageDTO params) {
        PageParams pageParams = params.toPageParams(
                new String[]{"orderNo","status","outTradeNo","goodsName","nickname","linkman"},
                new String[]{"s.order_no","s.status","s.out_trade_no","g.goods_name","u.nickname","s.linkman"},
                "s.create_time","beginTime","endTime");
        return Response.success(manager.getPage(pageParams));
    }

    @ApiPermission("export")
    @ApiOperation("导出订单")
    @PostMapping({"export_page"})
    public Response exportPage(@RequestBody ShopOrderExportDTO params) {
        ExportParams exportParams = params.toExportParams(
                new String[]{"orderNo","status","outTradeNo","goodsName","nickname","linkman"},
                new String[]{"s.order_no","s.status","s.out_trade_no","g.goods_name","u.nickname","s.linkman"},
                "s.create_time","beginTime","endTime");
        return manager.exportShopOrderPage(exportParams);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @ApiPermission("get")
    @ApiOperation("查询订单详情")
    @GetMapping("view/{id}")
    public Response<ShopOrderDetailVO> getOrderDetail(@PathVariable String id){
        return Response.success(manager.getOrderDetail(id));
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @ApiPermission("cancel")
    @ApiOperation("取消订单")
    @PutMapping("cancel/{id}")
    public Response cancel(@PathVariable String id){
        return Response.success(manager.cancel(id));
    }

    /**
     * 发货
     * @param dto
     * @return
     */
    @ApiPermission("delivery")
    @ApiOperation("发货")
    @PostMapping("delivery")
    public Response delivery(@RequestBody @Valid  DeliveryDTO dto){
        return Response.success(manager.delivery(dto));
    }

    /**
     * 查询快递轨迹
     * @param id
     * @return
     */
    @ApiPermission("track")
    @ApiOperation("查询快递轨迹")
    @GetMapping("track/{id}")
    public Response getExpressTrack(@ApiParam("订单ID") @PathVariable String id){
        return Response.success(manager.getExpressTrack(id));
    }
}
